package com.example.take_out_order.handle;


import com.example.take_out_order.common.Result;
import com.example.take_out_order.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.io.IOException;

@Slf4j
@RestControllerAdvice
public class BaseExceptionHandler {


    /**
     * 处理Assert的异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) throws IOException {
        log.error("Assert异常:-------------->{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result<Object> businessExceptionHandler(BusinessException e) {
        return Result.error(e.getResponse());
    }


    /**
     * ico
     */
    @ExceptionHandler(NoResourceFoundException.class)
    public Result<Object> ico(Exception e) {
        // log.error("NoResourceFoundException:-------------->", e);
        return Result.error("没ico 小事可以忽略");
    }

    /**
     * 运行时异常
     */
    @ExceptionHandler(Exception.class)
    public Result<Object> exceptionHandler(Exception e) {
        log.error("运行时异常:-------------->", e);
        e.printStackTrace();
        // return Result.error(ResponseEnum.UNKNOWN_ERROR);
        return Result.error(e.getMessage());
    }
}
